/**
 * 
 */
package com.budingge.httpuitls;

import java.awt.HeadlessException;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

/**
 * http工具类，获取url文件名，url文件大小,url返回码
 * 
 * @author budingge.com
 * 
 */
public class HttpUtils {

	private URL url;
	private Proxy proxy;
	private HttpURLConnection httpConn;
	private final int CONNECTION_TIMEOUT = 30 * 1000;
	private final int READ_TIMEOUT = 120 * 1000;

	
	
	/**
	 * 请求连接
	 * @return
	 */
	public HttpURLConnection getHttpConn() {
		return httpConn;
	}

	public HttpUtils(URL url) throws Exception {
		this(url, null);
	}

	public HttpUtils(URL url, Proxy proxy) throws Exception {
		super();
		this.url = url;
		this.proxy = proxy;
		init();
		doRequst();
	}

	private void init() throws Exception {
		initHttpURLConnection();
	}

	private void initHttpURLConnection() throws Exception {
		boolean httpsFlag = false;
		if (url.toString().toLowerCase().contains("https://")) {
			HttpsHandler.trustAllHttpsCertificates();
			HostnameVerifier hv = new HostnameVerifier() {
				public boolean verify(String urlHostName, SSLSession session) {
					return true;
				}
			};
			HttpsURLConnection.setDefaultHostnameVerifier(hv);
			httpsFlag = true;
		}
		URLConnection conn = proxy == null ? url.openConnection() : url
				.openConnection(proxy);
		if (httpsFlag) {
			httpConn = (HttpsURLConnection) conn;
		} else {
			httpConn = (HttpURLConnection) conn;
		}
	}

	public void doRequst() {
		httpConn.addRequestProperty("User-Agent",
				UserAgentUtils.getRandomUserAgent());
		httpConn.setConnectTimeout(CONNECTION_TIMEOUT);// 1/2分钟
		httpConn.setReadTimeout(READ_TIMEOUT);// 2分钟
	}

	/**
	 * 计算下载文件的大小http(s)
	 * 
	 * @param url
	 * @return
	 * @throws IOException
	 * @throws NoSuchAlgorithmException
	 * @throws KeyManagementException
	 */
	public long getContentLength(URL url) throws IOException {
		long value = -1;
		int code = httpConn.getResponseCode();
		if (code == HttpURLConnection.HTTP_OK) {
			value = httpConn.getContentLength();
		}
		return value;
	}

	/**
	 * http return code
	 * 
	 * @return
	 * @throws IOException
	 */
	public int getHttpCode() throws IOException {
		int code = httpConn.getResponseCode();
		return code;
	}

	/**
	 * 最后修改时间
	 * 
	 * @return
	 * @throws IOException
	 */
	public long getLastModified() throws IOException {
		long value = -1;
		int code = httpConn.getResponseCode();
		if (code == 200) {
			value = httpConn.getLastModified();
		}
		return value;
	}

	/**
	 * 获取网页的类型
	 * 
	 * @param strUrl
	 * @return
	 * @throws IOException
	 */
	public String getcontentType(String strUrl) throws IOException {
		String content_type = null;
		int code = httpConn.getResponseCode();
		if (code == 200) {
			content_type = httpConn.getContentType();
		}
		
		return content_type;
	}
	
	/**
	 * 全部头信息
	 * @return
	 */
	public Map<String, List<String>> getHeaderFields() {
		return httpConn.getHeaderFields();
	}
	
	
	
	

}
